﻿package frEngine.primitives.object2d
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import flash.display.*;
    import flash.geom.*;
    import frEngine.core.*;
    import frEngine.manager.*;
    import frEngine.primitives.*;
    import frEngine.shader.filters.fragmentFilters.*;

    public class Img2D extends FrQuad
    {
        public var oldBgBitmapData:BitmapData;
        public var newBgBitmapData:BitmapData;
        public var rect:Rectangle;
        private var _transparent:Boolean;
        protected var _offsetX:Number = 0;
        protected var _offsetY:Number = 0;
        public var autoDispose:Boolean = true;
        private var _needUvScaleFile:Boolean;

        public function Img2D(param1:String, param2:BitmapData, param3:Rectangle, param4:Boolean, param5:Boolean = true)
        {
            super(param1, 0, 0, param3.width, param3.height, false, null, null);
            material.materialParams.depthWrite = false;
            material.materialParams.colorKillvalue = 0;
            this._transparent = param4;
            this._needUvScaleFile = param5;
            if (this._transparent)
            {
                this.setMateiralBlendMode(EBlendType.BLEND_ALPHA0);
            }
            this.bitmapData = param2;
            this.initRect(param3);
            return;
        }// end function

        override public function getPosition(param1:Boolean = true, param2:Vector3D = null) : Vector3D
        {
            if (param2 == null)
            {
                param2 = new Vector3D();
            }
            param2.x = this._offsetX + this.x;
            param2.y = this._offsetY + this.y;
            return param2;
        }// end function

        public function setOffsetXY(param1:Number, param2:Number) : void
        {
            this._offsetX = param1;
            this._offsetY = param2;
            _toChange = true;
            return;
        }// end function

        public function set bitmapData(param1:BitmapData) : void
        {
            this.oldBgBitmapData = param1;
            return;
        }// end function

        public function get bitmapData() : BitmapData
        {
            return this.oldBgBitmapData;
        }// end function

        public function initRect(param1:Rectangle) : void
        {
            this.rect = param1;
            this.disposeTexture();
            if (this.rect.width > 1024)
            {
                this.rect.width = 1024;
            }
            if (this.rect.height > 1024)
            {
                this.rect.height = 1024;
            }
            var _loc_2:* = calculateBgWH(this.rect.width, this.rect.height);
            this.newBgBitmapData = new BitmapData(_loc_2.x, _loc_2.y, this._transparent, 0);
            var _loc_3:* = new Matrix();
            _loc_3.translate(-this.rect.x, -this.rect.y);
            this.newBgBitmapData.draw(this.oldBgBitmapData, _loc_3);
            var _loc_4:* = material.materialParams.uvValue2;
            if (this._needUvScaleFile)
            {
                _loc_4[0] = this.rect.width / _loc_2.x;
                _loc_4[1] = this.rect.height / _loc_2.y;
            }
            else
            {
                _loc_4[0] = 1;
                _loc_4[1] = 1;
            }
            this.setMaterial(this.newBgBitmapData, 0, "Rect3dBg");
            return;
        }// end function

        override protected function setTextureFilter(param1, param2:int) : FragmentFilter
        {
            var _loc_3:* = Resource3dManager.instance.getTexture3d(param1, param2);
            _loc_3.immediatelyUpload();
            param1 = new TextureFilter(_loc_3);
            return param1;
        }// end function

        public function disposeTexture() : void
        {
            var _loc_1:Texture3D = null;
            if (this.newBgBitmapData)
            {
                _loc_1 = Resource3dManager.instance.hasTexture3d(this.newBgBitmapData, 0);
                this.newBgBitmapData = null;
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.disposeTexture();
            renderList = null;
            removedFromScene();
            super.dispose(param1);
            return;
        }// end function

        public static function calculateBgWH(param1:int, param2:int) : Point
        {
            var _loc_3:int = 1;
            while (_loc_3 < param1)
            {
                
                _loc_3 = _loc_3 << 1;
                if (_loc_3 > 1024)
                {
                    break;
                }
            }
            var _loc_4:int = 1;
            while (_loc_4 < param2)
            {
                
                _loc_4 = _loc_4 << 1;
                if (_loc_4 > 1024)
                {
                    break;
                }
            }
            return new Point(_loc_3, _loc_4);
        }// end function

    }
}
